package com.hnzbxy.api.biz.converter;


import com.hnzbxy.api.biz.entity.Department;
import com.hnzbxy.api.biz.vo.DepartmentEditVO;
import com.hnzbxy.api.biz.vo.DepartmentVO;
import com.hnzbxy.api.biz.vo.DeptNodeVO;
import com.hnzbxy.api.biz.vo.DeptTransferItemVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门对象转换器
 * 用于PO与VO之间的转换
 *
 * @author xiaoge
 * @date 2020/3/7 17:00
 * @version 1.0
 */
public class DepartmentConverter {

    /**
     * 将PO转换为VO
     *
     * @param department 部门PO对象
     * @return 部门VO对象
     */
    public static DepartmentVO converterToVO(Department department) {
        if (department == null) {
            return null;
        }
        
        DepartmentVO vo = new DepartmentVO();
        vo.setId(department.getId());
        vo.setDeptCode(department.getDeptCode());
        vo.setDeptName(department.getDeptName());
        vo.setDeptAbbr(department.getDeptAbbr());
        vo.setParentDeptId(department.getParentDeptId());
        vo.setLeaderEmpId(department.getLeaderEmpId());
        vo.setDeptStatus(department.getDeptStatus());
        vo.setEstablishDate(department.getEstablishDate());
        vo.setDissolveDate(department.getDissolveDate());
        vo.setCreatorId(department.getCreatorId());
        vo.setCreateTime(department.getCreateTime());
        vo.setUpdaterId(department.getUpdaterId());
        vo.setUpdateTime(department.getUpdateTime());
        vo.setRemark(department.getRemark());
        
        return vo;
    }

    /**
     * 将VO转换为PO
     *
     * @param vo 部门VO对象
     * @return 部门PO对象
     */
    public static Department converterToPO(DepartmentVO vo) {
        if (vo == null) {
            return null;
        }
        
        Department department = new Department();
        department.setId(vo.getId());
        department.setDeptCode(vo.getDeptCode());
        department.setDeptName(vo.getDeptName());
        department.setDeptAbbr(vo.getDeptAbbr());
        department.setParentDeptId(vo.getParentDeptId());
        department.setLeaderEmpId(vo.getLeaderEmpId());
        department.setDeptStatus(vo.getDeptStatus());
        department.setEstablishDate(vo.getEstablishDate());
        department.setDissolveDate(vo.getDissolveDate());
        department.setCreatorId(vo.getCreatorId());
        department.setUpdaterId(vo.getUpdaterId());
        department.setRemark(vo.getRemark());
        
        return department;
    }

    /**
     * 将PO转换为节点VO
     *
     * @param department 部门PO对象
     * @return 部门节点VO对象
     */
    public static DeptNodeVO converterToNode(Department department) {
        if (department == null) {
            return null;
        }
        
        DeptNodeVO node = new DeptNodeVO();
        node.setId(department.getId());
        node.setDeptCode(department.getDeptCode());
        node.setDeptName(department.getDeptName());
        node.setDeptAbbr(department.getDeptAbbr());
        node.setParentDeptId(department.getParentDeptId());
        node.setDeptStatus(department.getDeptStatus());
        
        return node;
    }

    /**
     * 将PO转换为编辑VO
     *
     * @param department 部门PO对象
     * @return 部门编辑VO对象
     */
    public static DepartmentEditVO converterToEditVO(Department department) {
        if (department == null) {
            return null;
        }
        
        DepartmentEditVO editVO = new DepartmentEditVO();
        editVO.setId(department.getId());
        editVO.setDeptCode(department.getDeptCode());
        editVO.setDeptName(department.getDeptName());
        editVO.setDeptAbbr(department.getDeptAbbr());
        editVO.setParentDeptId(department.getParentDeptId());
        editVO.setLeaderEmpId(department.getLeaderEmpId());
        editVO.setDeptStatus(department.getDeptStatus());
        editVO.setEstablishDate(department.getEstablishDate());
        editVO.setDissolveDate(department.getDissolveDate());
        editVO.setRemark(department.getRemark());
        
        return editVO;
    }

    /**
     * 将PO转换为编辑PO
     *
     * @param editVO 部门编辑VO对象
     * @return 部门PO对象
     */
    public static Department converterToPO(DepartmentEditVO editVO) {
        if (editVO == null) {
            return null;
        }
        
        Department department = new Department();
        department.setDeptCode(editVO.getDeptCode());
        department.setDeptName(editVO.getDeptName());
        department.setDeptAbbr(editVO.getDeptAbbr());
        department.setParentDeptId(editVO.getParentDeptId());
        department.setLeaderEmpId(editVO.getLeaderEmpId());
        department.setDeptStatus(editVO.getDeptStatus());
        department.setEstablishDate(editVO.getEstablishDate());
        department.setDissolveDate(editVO.getDissolveDate());
        department.setRemark(editVO.getRemark());
        
        return department;
    }

    /**
     * 将PO列表转换为部门选择项列表
     *
     * @param departments 部门PO列表
     * @return 部门选择项列表
     */
    public static List<DeptTransferItemVO> converterToDeptTransferItem(List<Department> departments) {
        if (departments == null || departments.isEmpty()) {
            return null;
        }
        
        return departments.stream().map(dept -> {
            DeptTransferItemVO item = new DeptTransferItemVO();
            item.setKey(dept.getId());
            item.setLabel(dept.getDeptName());
            item.setDeptCode(dept.getDeptCode());
            item.setParentDeptId(dept.getParentDeptId());
            return item;
        }).collect(Collectors.toList());
    }
}
    